<?php
namespace app\api\server\uploade;
use app\admin\ServerBase;
use app\logic\traits\AliyunOss;
use think\Request;
use think\facade\Db;
use app\admin\common;
use app\admin\server\uploade\AliYunKey;
use think\exception\ValidateException;

class Uploade extends ServerBase{

    protected $request;
    protected $addSettingParam=array();
    protected static $s_model_aliyun;
    use AliyunOss;
    public function __construct(Request $request)
    {
        $this->checkAuth();
        $this->request = $request;
        self::$s_model_aliyun=new AliYunKey($this->request);
    }

    public function LayUploadFile(){

        if(!request()->file('file')){
            $this->getMessageCode('adminMessage.excel.import.fileTrue',1);
            return false;
        }
        try{
            $res=common::getImportDataFile();
            $path=$res['path'];
            $arr=explode(DIRECTORY_SEPARATOR,$path);
            $name = $arr[count($arr)-1];
            $name_arr = explode('.',$name);
            $suffix = $name_arr[1];
            $save_name='site/'.date('Y-m-d').'/'.$name;
            $bucket = 'jcd-music';//音视频
            if(!in_array($suffix,['mp3','mp4'])){
                $this->getMessageCode("adminMessage.file.uploade.success",2);
                return $res['url'];
            }
            $ossRes=$this->uploads($path,$save_name,$bucket);
            if($ossRes['code']==-1){
                // 验证失败 输出错误信息\
                $this->getMessageCode('adminMessage.file.uploade.error',1);
                $this->error=$ossRes['msg'];
            }
            //处理图片
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息\
            $this->getMessageCode('adminMessage.file.uploade.error',1);
            $this->error=$this->error.','.$e->getError();
            return false;
        }
        $this->getMessageCode("adminMessage.file.uploade.success",2);
        return $ossRes['msg'];
    }

    public function LayUploadVideo(){
        if(!request()->file('file')){
            $this->getMessageCode('adminMessage.excel.import.fileTrue',1);
            return false;
        }
        try{
            $res=common::getImportDataFile();
            //处理图片
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息\
            $this->getMessageCode('adminMessage.file.uploade.error',1);
            $this->error=$this->error.','.$e->getError();
            return false;
        }
        $this->getMessageCode("adminMessage.file.uploade.success",2);
        return $res['url'];
    }

    public function uploadeImage(){
        if(!request()->file('file')){
            $this->getMessageCode('adminMessage.excel.import.fileTrue',1);
            return false;
        }
        try{
            //is_thum 0不开启缩略图1开启
            $path=common::getImportDataFileNew('admin', 'admin','',config('key.is_open_thum'));
            //处理图片
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息\
            $this->getMessageCode('adminMessage.file.uploade.error',1);
            $this->error=$this->error.','.$e->getError();
            return false;
        }
        if(!($_new_path=self::$s_model_aliyun->uploadeAliYun($path['path'],$path['name']))){
            $this->code=self::$s_model_aliyun->code;
            $this->error=self::$s_model_aliyun->error;
            return false;
        }
        $this->getMessageCode("adminMessage.file.uploade.success",2);
        return $_new_path;
    }
    /**
     * 导入用户
     *
     */
    public function import(){
        if(!request()->file('file')){
            $this->getMessageCode('adminMessage.excel.import.fileTrue',1);
            return false;
        }
        try{
            $path=common::getImportData();
            $excel_data=common::importData($path);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息\
            $this->getMessageCode('adminMessage.excel.import.get_data',1);
            $this->error=$this->error.','.$e->getError();
            return false;
        }
        $data_insert=[];
        $number=0;
        foreach($excel_data as $k=>$v){
            //1.查找当前该 手机号是否在平台注册
            $checkMobileIsTrue=self::$model->findUser(['mobile'=>$v['mobile']],'id');
            if(!$checkMobileIsTrue){
                continue;
            }
            $data_insert[$number]['id']=$checkMobileIsTrue['id'];
            $data_insert[$number]['user_insert']['mobile']=$v['mobile'];
            $data_insert[$number]['user_insert']['create_time']=time();
            $data_insert[$number]['userinfo_insert']['username']=$v['idcard'];
            $data_insert[$number]['userinfo_insert']['idCard']=$v['username'];
            $number++;
        }
        if(!$data_insert){
            $this->getMessageCode('message.excel.import.userImport.noUser',1);
            return false;
        }
        Db::startTrans();
        //把查找到的用户更新身份证号
        foreach($data_insert as $k=>$v){
            $up=$v['userinfo_insert'];
            $where=['user_id'=>$v['id']];
            $r=self::$model->updateInfo($up,$where);
            if(!$r){
                $this->getMessageCode('message.curd_message.editError',1);
                $this->error.="|手机号为：".$v['user_insert'];
                Db::rollback();
                return false;
            }
        }
        $this->getMessageCode('adminMessage.excel.import.success');
        Db::commit();
        return true;
    }



}
?>